package dialog.program;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("rawtypes")
class Document
{
	int documentCode;
	String title;
	String authors;
	String ISBNCode;
	String type;
	Loan loan = null;
	Collection loansForJournal = new LinkedList();
	static Map usedPositions = new HashMap();
	
	@SuppressWarnings("unchecked")
	public Document(String title) 
	{
		int nextDocumentCodeAvailable = 0;
		this.title = title;
		ISBNCode = "";
		authors = "";
		type = "";
		documentCode = nextDocumentCodeAvailable++;
		while (usedPositions.containsValue(documentCode))
		{
			documentCode++;
			nextDocumentCodeAvailable++;			
		}
		usedPositions.put(documentCode, documentCode);
	}
	
	public void removePosition(int position)
	{
		usedPositions.remove(position);
	}
	
   	public boolean Equals(Object obj)
	{
		Document doc = (Document)obj;
		return documentCode == doc.documentCode;
	}
	
	public boolean isAvailable() 
	{
		return loan == null;
	}
	
	public boolean isOut() 
	{
		return ! isAvailable() ;
	}
	
    public User getBorrower() 
    {
    	if (loan != null)
    	{
    		return loan.getUser();
    	}
    	else return null;
    }

    public int getCode()
    {
    	return documentCode;
    }

    public String getTitle() 
    {
    	return title;
    }
    
    public String getType() 
    {
    	return type;
    }

    public String getAuthors() 
    {
    	return authors;
    }

    public String getISBN() 
    {
    	return ISBNCode;
    }

    public void addLoan(Loan loan) 
    {
    	this.loan = loan;
    }
    
    @SuppressWarnings("unchecked")
	public void addLoanForJournal(Loan loan)
    {
    	loansForJournal.add(loan);
    }
    
    public void removeLoanForJournal(Loan loan) 
    {
    	loansForJournal.remove(loan);
    }

    public void removeLoan() 
    {
    	loan = null;
    }

    protected void printHeader(HttpServletResponse response)
    	throws IOException, ServletException
    {
    	PrintWriter writer = response.getWriter();
    	writer.println("<center>Document ID: " + (getCode() + 1) + "<br>");
    	writer.println("Type: " + getType() + "<br>");
    	writer.println("Title: " + getTitle() + "<br>");
    	if (type != "journal")
    	{
    		writer.println("Author(s): " + getAuthors() + "<br>");
    	}
    }
    
    protected void printAvailability(HttpServletResponse response) 
    	throws IOException, ServletException
    {
    	PrintWriter writer = response.getWriter();
    	if (loan == null) 
    	{
    		writer.println("<center><b>Is available</b><br>");
    	}
    	else
    	{
    		User user = loan.getUser();
    		writer.println("<br><center><b>Is hold by:</b><br> User ID: <b>" + (user.getCode() + 1) + "</b><br> User name: <b>\"" + user.getName() + "\"</b><br>");
    	}
    }

    protected void printGeneralInfo(HttpServletResponse response)
    	throws IOException, ServletException
    {
    	PrintWriter writer = response.getWriter();
    	if (!getISBN().equals(""))
    	{
    		writer.println("<center>ISBN: " + getISBN() + "<br>");
    	}
    }
    
    protected void printInfo(HttpServletResponse response) 
    	throws IOException, ServletException
    {
    	PrintWriter writer = response.getWriter();
    	printHeader(response);        
    	printGeneralInfo(response);
    	if (type != "journal") printAvailability(response);
    	else if (loansForJournal.size() != 0)
    	{
    		if (type == "journal") writer.println("<center><b>Is available</b><br>");
    		if(loansForJournal.size() != 0) writer.println("<br><b>Is hold by: </b><br>");
			Iterator i = loansForJournal.iterator();
			while (i.hasNext())
			{
				Loan loan = (Loan)i.next();
				User user = loan.getUser();
				writer.println("<center>User ID: <b>" + (user.getCode() + 1) + "</b><br> User name: <b>\"" + user.getName() + "\"</b><br><br>");
			}
       	}
    }
}
